package com.pai4j.pgc.controller;

import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.common.enums.ResponseCodeEnum;
import com.pai4j.domain.openapi.OpenApiPageQueryDTO;
import com.pai4j.domain.openapi.OpenApiUpsertDTO;
import com.pai4j.domain.vo.OpenApiDetailVO;
import com.pai4j.log.annotation.OperationLog;
import com.pai4j.pgc.entity.OpenApiEntity;
import com.pai4j.pgc.service.OpenApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/open/admin/api")
public class OpenApiAdminController {

    @Autowired
    private OpenApiService openApiService;

    @PostMapping("/page")
    public PAIResponseBean<Page<OpenApiEntity>> page(@RequestBody OpenApiPageQueryDTO dto) {
        return PAIResponseBeanUtil.success(openApiService.page(dto));
    }

    @GetMapping("/{id}")
    public PAIResponseBean<OpenApiDetailVO> get(@PathVariable("id") Long id) {
        OpenApiDetailVO vo = openApiService.getDetail(id);
        if (vo == null) return PAIResponseBeanUtil.error(ResponseCodeEnum.ERROR.getCode(), "接口不存在");
        return PAIResponseBeanUtil.success(vo);
    }

    @PostMapping("/upsert")
    @OperationLog(module = "OPEN-API管理", operation = "接口新增更新", description = "创建或更新OpenAPI定义", recordParams = false)
    public PAIResponseBean<OpenApiDetailVO> upsert(@RequestBody OpenApiUpsertDTO dto,
                                                   @RequestHeader(value = "X-User", required = false) String operator) {
        if (operator == null || operator.isBlank()) operator = "system";
        OpenApiDetailVO saved = openApiService.upsert(dto, operator);
        return PAIResponseBeanUtil.success(saved);
    }

    @DeleteMapping("/{id}")
    @OperationLog(module = "OPEN-API管理", operation = "接口删除", description = "删除OpenAPI定义", recordParams = false)
    public PAIResponseBean<Boolean> delete(@PathVariable("id") Long id) {
        openApiService.delete(id);
        return PAIResponseBeanUtil.success(true);
    }

    @GetMapping("/export/{id}")
    public PAIResponseBean<String> export(@PathVariable("id") Long id,
                                          @RequestParam(value = "format", defaultValue = "json") String format) {
        try {
            if ("md".equalsIgnoreCase(format) || "markdown".equalsIgnoreCase(format)) {
                String md = openApiService.exportAsMarkdown(id);
                if (md == null) return PAIResponseBeanUtil.error(ResponseCodeEnum.ERROR.getCode(), "接口不存在");
                return PAIResponseBeanUtil.success(md);
            }
            String json = openApiService.exportAsJson(id);
            if (json == null) return PAIResponseBeanUtil.error(ResponseCodeEnum.ERROR.getCode(), "接口不存在");
            return PAIResponseBeanUtil.success(json);
        } catch (Exception e) {
            return PAIResponseBeanUtil.error(ResponseCodeEnum.ERROR.getCode(), e.getMessage());
        }
    }
}
